﻿@using System.Data;
@using FileServer.Common;
@using UtilsBase;
@{
    ViewBag.PageTitle = "洪峰智能";

    Layout = "~/Views/Shared/PageTemplate.cshtml";
}

<script>
    $(document).ready(function () {
        //  $("#sel_SearchType option[value='@(ViewBag.Type)']").attr("selected", true);

        if ("@(ViewBag.order)" == "upd") {
            getEdit("@(ViewBag.tp_guid)");
        }
        getVariable(0);
       
    });
    function getEdit(dguid) {
        $.post("getPlain", { "flag": Math.random(), guid: dguid }, function (result) {
            var myobj = (result.data);
            $("#tp_title").val(myobj.tp_title);
           
            for (var i = 0; i < document.getElementById("tp_status").options.length; i++) {
                if (document.getElementById("tp_status").options[i].value == myobj.tp_status) {
                    document.getElementById("tp_status").options[i].selected = true;
                    break;
                }
                else {
                    document.getElementById("tp_status").options[i].selected = false;

                }
            }

          
            getConditionList_main();
           
        });
    }


    function getHasVariableDevice(code) {
        $("#seldevice").empty();
        $("#seldevice").append("<option value=''  selected='selected'>请选择</option>");
        $.post("getHasVariableDevice", { "flag": Math.random(), code: code, p_guid: $("#tp_p_guid").val() }, function (result) {
            var myobj = (result.data);
            for (var i = myobj.length - 1; i >= 0; i--) {
                $("#seldevice").prepend("<option value='" + myobj[i].d_guid + "'>" + myobj[i].d_name + "</option>")
            }
        });
    }
    function getHasVariableDevice2() {
        $("#seldevice2").empty();
        $("#seldevice2").append("<option value=''  selected='selected'>请选择</option>");
        $.post("getHasVariableDevice", { "flag": Math.random(), export: 1, p_guid: $("#tp_p_guid").val() }, function (result) {
            var myobj = (result.data);
            for (var i = myobj.length - 1; i >= 0; i--) {
                $("#seldevice2").prepend("<option value='" + myobj[i].d_guid + "'>" + myobj[i].d_name + "</option>")
            }
        });
    }
    function getCommandByDevice(dguid) {
        $("#selcommand").empty();
        $("#selcommand").append("<option value=''  selected='selected'>请选择</option>");
        $.post("getCommandList", { "flag": Math.random(), guid: dguid }, function (result) {
            var myobj = (result.data);
            for (var i = myobj.length - 1; i >= 0; i--) {
                $("#selcommand").prepend("<option value='" + myobj[i].dc_guid + "'>" + myobj[i].dc_name + "</option>")
            }
        });
    }

    function getVariableByDevice2(dguid) {
        $("#selvar").empty();
        $("#selvar").append("<option value=''  selected='selected'>请选择</option>");
        $.post("getVariableByDevice", { "flag": Math.random(), guid: dguid, export: 0 }, function (result) {
            var myobj = (result.data);
            for (var i = myobj.length - 1; i >= 0; i--) {
                $("#selvar").prepend("<option value='" + myobj[i].v_guid + "'>" + myobj[i].v_name + "</option>")
            }
        });
    }

    //function isDelay(dguid) {
    //    $.post("getDeviceModel", { "flag": Math.random(), guid: dguid }, function (result) {
    //        var myobj = (result.data);
    //        //延时
    //        $("#lc_value").val("");
    //        if (myobj.m_code == "1017") {
    //            //延时

    //            document.getElementById("divlcvalue").style.display = "";
    //        }
    //        else {
    //            document.getElementById("divlcvalue").style.display = "none";
    //        }



    //    });
    // }


    function getVariableByDevice2(dguid) {
        $("#selvar").empty();
        $("#selvar").append("<option value=''  selected='selected'>请选择</option>");
        $.post("getVariableByDevice", { "flag": Math.random(), guid: dguid, export: 1 }, function (result) {
            var myobj = (result.data);
            for (var i = myobj.length - 1; i >= 0; i--) {
                $("#selvar").prepend("<option value='" + myobj[i].v_guid + "'>" + myobj[i].v_name + "</option>")
            }
        });
    }
    function getVariableValueRlue(dguid) {
        $("#selvarvalue").empty();
        $("#selvarvalue").append("<option value=''  selected='selected'>请选择</option>");
        $.post("getVariableValueRlue", { "flag": Math.random(), guid: dguid, export: 0 }, function (result) {
            var myobj = (result.data);
            for (var i = myobj.length - 1; i >= 0; i--) {
                $("#selvarvalue").prepend("<option value='" + myobj[i].vr_guid + "'>" + myobj[i].vr_name + "</option>")
            }
        });
    }
    function getLinkageList() {
        $("#selLinkage").empty();
        $("#selLinkage").append("<option value=''  selected='selected'>请选择</option>");
        $.post("getLinkageList", { "flag": Math.random() }, function (result) {
            var myobj = (result.data);
            for (var i = myobj.length - 1; i >= 0; i--) {
                $("#selLinkage").prepend("<option value='" + myobj[i].l_guid + "'>" + myobj[i].l_title + "</option>")
            }
        });
    }

    function getVariable(type) {
        if (type == 0) {
            document.getElementById("divVisible_device").style.display = "";
           // document.getElementById("divVisible_yanshi").style.display = "none";
            document.getElementById("divVisible_match").style.display = "none";
            document.getElementById("divVisible_chuanganqi").style.display = "none";
            getHasVariableDevice("");

        }
        else if (type == 100) {
            document.getElementById("divVisible_device").style.display = "none";
           // document.getElementById("divVisible_yanshi").style.display = "";
            document.getElementById("divVisible_match").style.display = "none";
            document.getElementById("divVisible_chuanganqi").style.display = "none";
        }
        else if (type == 2) {
            document.getElementById("divVisible_device").style.display = "none";
            //document.getElementById("divVisible_yanshi").style.display = "none";
            document.getElementById("divVisible_match").style.display = "";
            document.getElementById("divVisible_chuanganqi").style.display = "none";
            getHasVariableDevice2();
        }
        else if (type ==3) {
            document.getElementById("divVisible_device").style.display = "";
            document.getElementById("divVisible_chuanganqi").style.display = "";
            //document.getElementById("divVisible_yanshi").style.display = "none";
            document.getElementById("divVisible_match").style.display = "none";
            getHasVariableDevice("1001");
        }
    }



    function getConditionList_main() {
        $.post("getPlainItemList", { "flag": Math.random(), guid: $("#tp_guid").val(), type: "main" }, function (result) {
            var pagedata = JSON.parse(result).data;
            
            rowcount = 0;
            var htmlstr = "";
            document.getElementById("pagelist_main").innerHTML = "";


            htmlstr = htmlstr + "<tr class='plainItemTRTitle'>";
            htmlstr = htmlstr + "<td >序号</td >";
            htmlstr = htmlstr + "<td >id</td >";
            htmlstr = htmlstr + "<td >副码</td >";
            htmlstr = htmlstr + "<td >标题</td >";
            htmlstr = htmlstr + "<td >设备</td >";
            htmlstr = htmlstr + "<td >状态</td >";
            htmlstr = htmlstr + "<td >类型</td >";
            htmlstr = htmlstr + "<td >指令</td >";
            htmlstr = htmlstr + "<td >条件</td >";
            htmlstr = htmlstr + "<td >值</td >";

            htmlstr = htmlstr + "<td >排序</td >";
            htmlstr = htmlstr + "<td >操作</td >";
            htmlstr = htmlstr + "</tr >";

            if (pagedata != null && pagedata.length > 0) {
                for (var i = 0; i < pagedata.length; i++) {
                    rowcount = i + 1;
                    htmlstr = htmlstr + "<tr>";

                    var status = "启用";
                    if (pagedata[i].tpi_status == 1) {
                        status = "禁用";
                    }
                    var type = "";
                    if (pagedata[i].tpi_type == 0) {
                        type = "运行设备指令";
                    }
                    else if (pagedata[i].tpi_type == 100) {
                        type = "延时等待";
                    }
                    else if (pagedata[i].tpi_type == 2) {
                        type = "等待条件成立";
                    }
                    else if (pagedata[i].tpi_type == 1) {
                        type = "运行条件触发器";
                    }
                    else if (pagedata[i].tpi_type == 3) {
                        type = "传感器上传数据";
                    }
                    htmlstr = htmlstr + "<td >" + rowcount + "</td >";
                    htmlstr = htmlstr + "<td >" + pagedata[i].tpi_id + "</td >";
                    htmlstr = htmlstr + "<td >" + pagedata[i].tpi_p_model_no + "</td >";
                    
                    htmlstr = htmlstr + "<td >" + pagedata[i].tpi_title + "</td >";
                    htmlstr = htmlstr + "<td >" + status + "</td >";
                    htmlstr = htmlstr + "<td >" + pagedata[i].d_name + "</td >";
                    htmlstr = htmlstr + "<td >" + type + "</td >";
                    htmlstr = htmlstr + "<td >" + pagedata[i].tpi_key + "</td >";
                    htmlstr = htmlstr + "<td >" + pagedata[i].tpi_value_rlue + "</td >";
                    htmlstr = htmlstr + "<td >" + pagedata[i].tpi_value + "</td >";

                    htmlstr = htmlstr + "<td >" + pagedata[i].tpi_order + "</td >";
                    htmlstr = htmlstr + "<td ><a href=\"javascript:delItem('" + pagedata[i].tpi_guid + "')\">删除</a> </td >";
                    htmlstr = htmlstr + "</tr >";

                }
                document.getElementById("pagelist_main").innerHTML = htmlstr;
            }
        });
    }



    function delItem(guid) {
        var data = $("#addForm").serializeArray(); //自动将form表单封装成json
        $.post("delItem?guid=" + guid, data, function (result) {
            var jsonresult = (result);
            if (jsonresult.respCode == "200") {
                getConditionList_main();
            }
            else {
                alert(jsonresult.respMsg);
                return;
            }
        });

    }

    function saveAll() {
        var data = $("#addForm").serializeArray(); //自动将form表单封装成json
        $.post("saveAll", data, function (result) {
            var jsonresult = (result);
            if (jsonresult.respCode == "200") {
                // window.location.href = "list";
                getConditionList_main();
            }
            else {
                alert(jsonresult.respMsg);
                return;
            }
        });

    }

    function saveplain() {
        var data = $("#addForm").serializeArray(); //自动将form表单封装成json
        $.post("saveplain", data, function (result) {
            var jsonresult = (result);
            if (jsonresult.respCode == "200") {
                //   window.location.href = "list";
                document.getElementById("spantaskplain").innerHTML = "保存成功!";
                // $("#linkagelable").innerHTML="保存成功!";
            }
            else {
                alert(jsonresult.respMsg);
                return;
            }
        });

    }

    function selectCommand(commandval)
    {
         
        $.post("getCommand", { "flag": Math.random(), guid: commandval }, function (result) {
            var jsonresult = (result);
            if (jsonresult.respCode == "200") {
                if(jsonresult.data.dc_key =="delay")
                {
                    document.getElementById("divVisible_yanshi").style.display = "";
                }
               else
               {
                    document.getElementById("divVisible_yanshi").style.display = "none";
               }
            }
            else {
                alert(jsonresult.respMsg);
                return;
            }
        });
    }
</script>

<style>
    #pagelist_follow {
        width: 100%;
    }

    #pagelist_main {
        width: 100%;
    }

    .kv-item label {
        width: 100px;
    }

    .plainItemTable {
    }

        .plainItemTable td {
            line-height: 30px;
        }

    .plainItemTRTitle td {
        font-weight: bold;
        background-color:aliceblue;
    }
</style>
<div id="saper-container">
    <form id="addForm" name="addForm" action="list" method="post">
        <input type="hidden" name="tp_p_guid" id="tp_p_guid" value="@ViewBag.tp_p_guid" />
        <div id="saper-hd"></div>
        <div id="saper-bd">
            <div class="subfiled clearfix">
                <h2>流程设计</h2>
            </div>
            <div class="subfiled-content">

                <div id="" title="添加/编辑">
                    <form action="" name="addForm" id="addForm" method="post">

                        <div class="kv-item clearfix">
                            <label><span class="impInfo">*</span>标题：</label>
                            <div class="kv-item-content">
                                <input type="text" name="tp_title" id="tp_title" placeholder="标题" />
                                <input type="hidden" name="tp_guid" id="tp_guid" value="@ViewBag.tp_guid" />
                            </div>
                        </div>
                        <div class="kv-item clearfix">
                            <label><span class="impInfo">*</span>状态：</label>
                            <div class="kv-item-content">
                                <select id="tp_status" name="tp_status" style="width:130px;">
                                    <option value="0">启用</option>
                                    <option value="1">禁用</option>
                                </select>

                            </div>
                        </div>

                        
                        <div class="kv-item clearfix">
                            <label></label>
                            <div class="kv-item-content">
                                <input type="button" id="save" name="save" value="保存" onclick="javascript:saveplain()" />
                                <span id="spantaskplain"></span>
                            </div>
                        </div>
                     

                        <div><hr /></div>
                        <div style="background-color:antiquewhite;line-height:25px;padding:5px;font-weight: bold;">流程内容</div>
                      
                        <div style="width:100%;">
                            <div style="padding:10px;">
                                <div class="kv-item clearfix">
                                    <label><span class="impInfo">*</span>流程标题：</label>
                                    <div class="kv-item-content">
                                        <input type="text" name="tpi_title" id="tpi_title" placeholder="流程标题" />
                                        <input type="hidden" name="tpi_guid" id="tpi_guid" value="@ViewBag.tpi_guid" />
                                    </div>
                                </div>
                              
                                <div class="kv-item clearfix">
                                    <label>执行顺序：</label>
                                    <div class="kv-item-content">
                                        <input type="text" id="tpi_order" name="tpi_order" value="" style="width:110px;" />
                                    </div>
                                </div>

                                <div class="kv-item clearfix">
                                    <label>执行事项：</label>
                                    <div class="kv-item-content">
                                        <select id="tpi_type" name="tpi_type" style="width:130px;" onchange="getVariable(this.value)">
                                            <option value="0">运行设备指令</option>
                                         @*   <option value="100">延时等待</option>*@
                                            <option value="2">等待条件成立</option>
                                            <option value="3">传感器上传数据</option>
                                            @* <option value="1">运行条件触发器</option>*@
                                        </select>
                                        <input type="hidden" name="hid_tpi_type" id="hid_tpi_type" value="" />
                                    </div>
                                </div>
                            </div>
                            <div><hr /></div>
                            <div id="divVisible_device">
                                <div class="kv-item clearfix">
                                    <label>我的设备：</label>
                                    <div class="kv-item-content">
                                        <select id="seldevice" name="seldevice" style="width:130px;" onchange="getCommandByDevice(this.value)">
                                            <option value="">请选择</option>
                                        </select>
                                    </div>
                                </div>
                                <div class="kv-item clearfix">
                                    <label>设备指令：</label>
                                    <div class="kv-item-content">
                                        <select id="selcommand" name="selcommand" style="width:130px;" onchange="selectCommand(this.value)">
                                            <option value="">请选择</option>
                                        </select>
                                    </div>
                                </div>
                               
                            </div>
                            <div id="divVisible_chuanganqi" style="display:none">
                                <div class="kv-item clearfix">
                                    <label>执行次数：</label>
                                    <div class="kv-item-content">
                                        <input type="text" id="tpi_repeat_count" name="tpi_repeat_count" value="" style="width:110px;" />
                                        -1无限次、-100停止、或指定次数 1-N次
                                    </div>
                                </div>
                                <div class="kv-item clearfix">
                                    <label>每隔：</label>
                                    <div class="kv-item-content">
                                        <input type="text" id="tpi_repeat_interval" name="tpi_repeat_interval" value="" style="width:110px;" />
                                        (秒)执行一次
                                    </div>
                                </div>
                            </div>
                            <div id="divVisible_yanshi" style="display:none">
                                @*    //等待*@
                                <div class="kv-item clearfix">
                                    <label>延时（秒）：</label>
                                    <div class="kv-item-content">
                                        <input type="text" id="delaysecond" name="delaysecond" value="" style="width:110px;" />
                                    </div>
                                </div>
                            </div>
                            <div id="divVisible_match" style="display:none">
                                @*  //条件成立等待*@
                                <div class="kv-item clearfix">
                                    <label>我的设备：</label>
                                    <div class="kv-item-content">
                                        <select id="seldevice2" name="seldevice2" style="width:130px;" onchange="getVariableByDevice2(this.value)">
                                            <option value="">请选择</option>
                                        </select>
                                    </div>
                                </div>
                                <div class="kv-item clearfix">
                                    <label>设备变量：</label>
                                    <div class="kv-item-content">
                                        <select id="selvar" name="selvar" style="width:130px;" onchange="getVariableValueRlue(this.value);getVariable(this.value);">
                                            <option value="">请选择</option>
                                        </select>
                                    </div>
                                </div>

                                <div class="kv-item clearfix">
                                    <label>条件：</label>
                                    <div class="kv-item-content">
                                        <select id="selvarvalue" name="selvarvalue" style="width:130px;">
                                            <option value="">请选择</option>
                                        </select>
                                    </div>
                                </div>
                                <div class="kv-item clearfix">
                                    <label>条件阀值：</label>
                                    <div class="kv-item-content">
                                        <input type="text" id="conditionValue" name="conditionValue" value="" style="width:110px;" />
                                    </div>
                                </div>
                            </div>
                            @*   <div id="divVisible_trigger" >
                            //条件触发器
                            <div class="kv-item clearfix">
                            <label>触发器选择：</label>
                            <div class="kv-item-content">
                            <select id="selLinkage" name="selLinkage" style="width:130px;">
                            <option value="">请选择</option>
                            </select>
                            </div>
                            </div>

                            </div>*@


                            <div>
                                <table id="pagelist_main" class="plainItemTable"></table>
                            </div>

                        </div>


                        <div><hr /></div>
                        <div style="margin:0 auto;width:150px;">

                            <input type="button" id="save" name="save" value="保存流程" onclick="javascript:saveAll()" />
                            &nbsp;&nbsp; <input type="button" id="but_back1" name="but_back1" value="返回" onclick="javascript:window.location.href='list?p_guid=@(Context.Request.Query["p_guid"])'" />
                        </div>
                    </form>
                </div>
            </div>
        </div>
        <div id="saper-ft"></div>
    </form>
</div>

